//
//  Date+Extension.swift
//  CommonKit
//
//  Created by guohongqi on 2021/7/8.
//

import UIKit

/// 日期格式
public enum HQDateFormatter: String {
    case ym = "yyyy-MM"
    case ymd = "yyyy-MM-dd"
    case ymdHm = "yyyy-MM-dd HH:mm"
    case ymdHms = "yyyy-MM-dd HH:mm:ss"
    case md = "MM-dd"
    case dm = "dd-MM"
}


public extension Date {
    
    /// 时间戳转日期
    ///
    /// - Parameters:
    ///   - timeStamp: 时间戳（毫秒）
    ///   - formatter: 格式
    /// - Returns: 日期字符串
    static func HQtimeStampToString(_ timeStamp: String, formatter: HQDateFormatter) -> String {
        let string = NSString.init(string: timeStamp)
        let timeSta: TimeInterval = string.doubleValue / 1000
        let dfmatter = DateFormatter.init()
        dfmatter.dateFormat = formatter.rawValue
        let date = NSDate(timeIntervalSince1970: timeSta)
        return dfmatter.string(from: date as Date)
    }

    /// 获取当前 秒级 时间戳 - 10位
    var HQtimeStamp : String {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return "\(timeStamp)"
    }

    /// 获取当前 毫秒级 时间戳 - 13位
    var HQmilliStamp : String {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let millisecond = CLongLong(round(timeInterval*1000))
        return "\(millisecond)"
    }
    
}

